home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / pygtk / 2.0 / demos / appwindow.py < prev    next >
Text File  |  2006-01-20  |  12KB  |  307 lines

  1. #!/usr/bin/env python
  2. '''Application main window
  3.  
  4. Demonstrates a typical application window, with menubar, toolbar, statusbar.'''
  5. # pygtk version: Maik Hertha <maik.hertha@berlin.de>
  6.  
  7. import gobject
  8. import gtk
  9.  
  10. (
  11.   COLOR_RED,
  12.   COLOR_GREEN,
  13.   COLOR_BLUE
  14. ) = range(3)
  15.  
  16. (
  17.   SHAPE_SQUARE,
  18.   SHAPE_RECTANGLE,
  19.   SHAPE_OVAL,
  20. ) = range(3)
  21.  
  22. ui_info = \
  23. '''<ui>
  24.   <menubar name='MenuBar'>
  25.     <menu action='FileMenu'>
  26.       <menuitem action='New'/>
  27.       <menuitem action='Open'/>
  28.       <menuitem action='Save'/>
  29.       <menuitem action='SaveAs'/>
  30.       <separator/>
  31.       <menuitem action='Quit'/>
  32.     </menu>
  33.     <menu action='PreferencesMenu'>
  34.       <menu action='ColorMenu'>
  35.         <menuitem action='Red'/>
  36.         <menuitem action='Green'/>
  37.         <menuitem action='Blue'/>
  38.       </menu>
  39.       <menu action='ShapeMenu'>
  40.         <menuitem action='Square'/>
  41.         <menuitem action='Rectangle'/>
  42.         <menuitem action='Oval'/>
  43.       </menu>
  44.       <menuitem action='Bold'/>
  45.     </menu>
  46.     <menu action='HelpMenu'>
  47.       <menuitem action='About'/>
  48.     </menu>
  49.   </menubar>
  50.   <toolbar  name='ToolBar'>
  51.     <toolitem action='Open'/>
  52.     <toolitem action='Quit'/>
  53.     <separator/>
  54.     <toolitem action='Logo'/>
  55.   </toolbar>
  56. </ui>'''
  57.  
  58.  
  59. # It's totally optional to do this, you could just manually insert icons
  60. # and have them not be themeable, especially if you never expect people
  61. # to theme your app.
  62. def register_stock_icons():
  63.     ''' This function registers our custom toolbar icons, so they
  64.         can be themed.
  65.     '''
  66.     items = [('demo-gtk-logo', '_GTK!', 0, 0, '')]
  67.     # Register our stock items
  68.     gtk.stock_add(items)
  69.  
  70.     # Add our custom icon factory to the list of defaults
  71.     factory = gtk.IconFactory()
  72.     factory.add_default()
  73.  
  74.     import os
  75.     img_dir = os.path.join(os.path.dirname(__file__), 'images')
  76.     img_path = os.path.join(img_dir, 'gtk-logo-rgb.gif')
  77.     try:
  78.         pixbuf = gtk.gdk.pixbuf_new_from_file(img_path)
  79.  
  80.         # Register icon to accompany stock item
  81.  
  82.         # The gtk-logo-rgb icon has a white background, make it transparent
  83.         # the call is wrapped to (gboolean, guchar, guchar, guchar)
  84.         transparent = pixbuf.add_alpha(True, chr(255), chr(255),chr(255))
  85.         icon_set = gtk.IconSet(transparent)
  86.         factory.add('demo-gtk-logo', icon_set)
  87.  
  88.     except gobject.GError, error:
  89.         print 'failed to load GTK logo for toolbar'
  90.  
  91. class ApplicationMainWindowDemo(gtk.Window):
  92.     def __init__(self, parent=None):
  93.         register_stock_icons()
  94.  
  95.         # Create the toplevel window
  96.         gtk.Window.__init__(self)
  97.         try:
  98.             self.set_screen(parent.get_screen())
  99.         except AttributeError:
  100.             self.connect('destroy', lambda *w: gtk.main_quit())
  101.  
  102.         self.set_title(self.__class__.__name__)
  103.         self.set_default_size(200, 200)
  104.  
  105.         merge = gtk.UIManager()
  106.         self.set_data("ui-manager", merge)
  107.         merge.insert_action_group(self.__create_action_group(), 0)
  108.         self.add_accel_group(merge.get_accel_group())
  109.  
  110.         try:
  111.             mergeid = merge.add_ui_from_string(ui_info)
  112.         except gobject.GError, msg:
  113.             print "building menus failed: %s" % msg
  114.         bar = merge.get_widget("/MenuBar")
  115.         bar.show()
  116.  
  117.         table = gtk.Table(1, 4, False)
  118.         self.add(table)
  119.  
  120.         table.attach(bar,
  121.             # X direction #          # Y direction
  122.             0, 1,                      0, 1,
  123.             gtk.EXPAND | gtk.FILL,     0,
  124.             0,                         0);
  125.  
  126.         bar = merge.get_widget("/ToolBar")
  127.         bar.set_tooltips(True)
  128.         bar.show()
  129.         table.attach(bar,
  130.             # X direction #       # Y direction
  131.             0, 1,                   1, 2,
  132.             gtk.EXPAND | gtk.FILL,  0,
  133.             0,                      0)
  134.  
  135.         # Create document
  136.         sw = gtk.ScrolledWindow()
  137.         sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
  138.         sw.set_shadow_type(gtk.SHADOW_IN)
  139.  
  140.         table.attach(sw,
  141.             # X direction           Y direction
  142.             0, 1,                   2, 3,
  143.             gtk.EXPAND | gtk.FILL,  gtk.EXPAND | gtk.FILL,
  144.             0,                      0)
  145.  
  146.         contents = gtk.TextView()
  147.         contents.grab_focus()
  148.  
  149.         sw.add (contents)
  150.  
  151.         # Create statusbar
  152.         self.statusbar = gtk.Statusbar()
  153.         table.attach(self.statusbar,
  154.             # X direction           Y direction
  155.             0, 1,                   3, 4,
  156.             gtk.EXPAND | gtk.FILL,  0,
  157.             0,                      0)
  158.  
  159.         # Show text widget info in the statusbar
  160.         buffer = contents.get_buffer()
  161.         buffer.connect("changed", self.update_statusbar)
  162.         mark_set_callback = (lambda buffer, new_location, mark:
  163.             self.update_statusbar(buffer))
  164.  
  165.         # cursor moved
  166.         buffer.connect("mark_set", mark_set_callback)
  167.  
  168.         self.connect("window_state_event", self.update_resize_grip)
  169.         self.update_statusbar(buffer)
  170.  
  171.         self.show_all()
  172.  
  173.     def __create_action_group(self):
  174.         # GtkActionEntry
  175.         entries = (
  176.           ( "FileMenu", None, "_File" ),               # name, stock id, label
  177.           ( "PreferencesMenu", None, "_Preferences" ), # name, stock id, label
  178.           ( "ColorMenu", None, "_Color"  ),            # name, stock id, label
  179.           ( "ShapeMenu", None, "_Shape" ),             # name, stock id, label
  180.           ( "HelpMenu", None, "_Help" ),               # name, stock id, label
  181.           ( "New", gtk.STOCK_NEW,                      # name, stock id
  182.             "_New", "<control>N",                      # label, accelerator
  183.             "Create a new file",                       # tooltip
  184.             self.activate_action ),
  185.           ( "Open", gtk.STOCK_OPEN,                    # name, stock id
  186.             "_Open","<control>O",                      # label, accelerator
  187.             "Open a file",                             # tooltip
  188.             self.activate_action ),
  189.           ( "Save", gtk.STOCK_SAVE,                    # name, stock id
  190.             "_Save","<control>S",                      # label, accelerator
  191.             "Save current file",                       # tooltip
  192.             self.activate_action ),
  193.           ( "SaveAs", gtk.STOCK_SAVE,                  # name, stock id
  194.             "Save _As...", None,                       # label, accelerator
  195.             "Save to a file",                          # tooltip
  196.             self.activate_action ),
  197.           ( "Quit", gtk.STOCK_QUIT,                    # name, stock id
  198.             "_Quit", "<control>Q",                     # label, accelerator
  199.             "Quit",                                    # tooltip
  200.             self.activate_action ),
  201.           ( "About", None,                             # name, stock id
  202.             "_About", "<control>A",                    # label, accelerator
  203.             "About",                                   # tooltip
  204.             self.activate_about ),
  205.           ( "Logo", "demo-gtk-logo",                   # name, stock id
  206.              None, None,                              # label, accelerator
  207.             "GTK+",                                    # tooltip
  208.             self.activate_action ),
  209.         );
  210.  
  211.         # GtkToggleActionEntry
  212.         toggle_entries = (
  213.           ( "Bold", gtk.STOCK_BOLD,                    # name, stock id
  214.              "_Bold", "<control>B",                    # label, accelerator
  215.             "Bold",                                    # tooltip
  216.             self.activate_action,
  217.             True ),                                    # is_active
  218.         )
  219.  
  220.         # GtkRadioActionEntry
  221.         color_entries = (
  222.           ( "Red", None,                               # name, stock id
  223.             "_Red", "<control><shift>R",               # label, accelerator
  224.             "Blood", COLOR_RED ),                      # tooltip, value
  225.           ( "Green", None,                             # name, stock id
  226.             "_Green", "<control><shift>G",             # label, accelerator
  227.             "Grass", COLOR_GREEN ),                    # tooltip, value
  228.           ( "Blue", None,                              # name, stock id
  229.             "_Blue", "<control><shift>B",              # label, accelerator
  230.             "Sky", COLOR_BLUE ),                       # tooltip, value
  231.         )
  232.  
  233.         # GtkRadioActionEntry
  234.         shape_entries = (
  235.           ( "Square", None,                            # name, stock id
  236.             "_Square", "<control><shift>S",            # label, accelerator
  237.             "Square",  SHAPE_SQUARE ),                 # tooltip, value
  238.           ( "Rectangle", None,                         # name, stock id
  239.             "_Rectangle", "<control><shift>R",         # label, accelerator
  240.             "Rectangle", SHAPE_RECTANGLE ),            # tooltip, value
  241.           ( "Oval", None,                              # name, stock id
  242.             "_Oval", "<control><shift>O",              # label, accelerator
  243.             "Egg", SHAPE_OVAL ),                       # tooltip, value
  244.         )
  245.  
  246.         # Create the menubar and toolbar
  247.         action_group = gtk.ActionGroup("AppWindowActions")
  248.         action_group.add_actions(entries)
  249.         action_group.add_toggle_actions(toggle_entries)
  250.         action_group.add_radio_actions(color_entries, COLOR_RED, self.activate_radio_action)
  251.         action_group.add_radio_actions(shape_entries, SHAPE_OVAL, self.activate_radio_action)
  252.  
  253.         return action_group
  254.  
  255.     def activate_about(self, action):
  256.         dialog = gtk.AboutDialog()
  257.         dialog.set_name("PyGTK Demo")
  258.         dialog.set_copyright("\302\251 Copyright 200x the PyGTK Team")
  259.         dialog.set_website("http://www.pygtk.org./")
  260.         ## Close dialog on user response
  261.         dialog.connect ("response", lambda d, r: d.destroy())
  262.         dialog.show()
  263.  
  264.     def activate_action(self, action):
  265.         dialog = gtk.MessageDialog(self, gtk.DIALOG_DESTROY_WITH_PARENT,
  266.             gtk.MESSAGE_INFO, gtk.BUTTONS_CLOSE,
  267.             'You activated action: "%s" of type "%s"' % (action.get_name(), type(action)))
  268.         # Close dialog on user response
  269.         dialog.connect ("response", lambda d, r: d.destroy())
  270.         dialog.show()
  271.  
  272.     def activate_radio_action(self, action, current):
  273.         active = current.get_active()
  274.         value = current.get_current_value()
  275.  
  276.         if active:
  277.             dialog = gtk.MessageDialog(self, gtk.DIALOG_DESTROY_WITH_PARENT,
  278.                 gtk.MESSAGE_INFO, gtk.BUTTONS_CLOSE,
  279.                 "You activated radio action: \"%s\" of type \"%s\".\nCurrent value: %d" %
  280.                 (current.get_name(), type(current), value))
  281.  
  282.             # Close dialog on user response
  283.             dialog.connect("response", lambda d, r: d.destroy())
  284.             dialog.show()
  285.  
  286.     def update_statusbar(self, buffer):
  287.         # clear any previous message, underflow is allowed
  288.         self.statusbar.pop(0)
  289.         count = buffer.get_char_count()
  290.         iter = buffer.get_iter_at_mark(buffer.get_insert())
  291.         row = iter.get_line()
  292.         col = iter.get_line_offset()
  293.         self.statusbar.push(0,
  294.         'Cursor at row %d column %d - %d chars in document' % (row, col, count))
  295.  
  296.     def update_resize_grip(self, widget, event):
  297.         mask = gtk.gdk.WINDOW_STATE_MAXIMIZED | gtk.gdk.WINDOW_STATE_FULLSCREEN
  298.         if (event.changed_mask & mask):
  299.             self.statusbar.set_has_resize_grip(not (event.new_window_state & mask))
  300.  
  301. def main():
  302.     ApplicationMainWindowDemo()
  303.     gtk.main()
  304.  
  305. if __name__ == '__main__':
  306.     main()
  307.